/**
 * 对 echarts 常用方法的封装
 * @author 董其宾
 * @time 2022-5-26 09:13:22
 */
function EchartsSelf($) {
    this.$ = $;

    this.myChart = null;

    this.init = function(id, title) {
        this.myChart = echarts.init(document.getElementById(id))
        const option = {
            title: {
                text: title
            },
            tooltip: {
                trigger: 'axis'
            },
            grid: {
                left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
            },
            yAxis: {
                type: 'value'
            },
            series:[]
        }
        this.myChart.setOption(option)
        return this
    }

    this.barLateralInit = function(id, title) {
        this.init(id, title)
        const option = {
            xAxis:{
                type: 'value'
            },
            yAxis:{
                type:'category'
            }
        }
        this.myChart.setOption(option)
        return this
    }

    this.lineGetData = function(url, key, options) {
        key = key || {}
        const that = this
        options = options || null
        this.$.getJSON(url, {key:key}, function(d) {
            if(d.code != 0) {
                layer.msg(d.msg)
                return false
            }

            const data = d.data
            const x = data.x
            const y = data.y

            that.myChart.setOption({
                xAxis: {
                    data:x,
                    axisLabel: { interval: 0, rotate: 30 }
                },
                series: [
                    {
                        type: 'line',
                        //smooth: true,
                        data: y,
                        itemStyle: {
                            normal: {
                                label: {
                                    show: true, //开启显示
                                    position: 'top', //在上方显示
                                    textStyle: { //数值样式
                                        color: 'black',
                                        fontSize: 16
                                    }
                                }
                            }
                        }
                    }
                ]
            })
            if(options) {
                that.myChart.setOption(options)
            }
        })
        return this
    }

    this.barGetData = function(url, key, options) {
        key = key || {}
        const that = this
        options = options || null
        this.$.getJSON(url, {key:key}, function(d) {
            if(d.code != 0) {
                layer.msg(d.msg)
                return false
            }

            const data = d.data
            const x = data.x
            const y = data.y

            that.myChart.setOption({
                xAxis: {
                    data:x,
                    axisLabel: { interval: 0, rotate: 30 }
                },
                series: [
                    {
                        type: 'bar',
                        data: y,
                        backgroundStyle: {
                            color: 'rgba(180, 180, 180, 0.2)'
                        },
                        itemStyle: {
                            normal: {
                                label: {
                                    show: true, //开启显示
                                    position: 'top', //在上方显示
                                    textStyle: { //数值样式
                                        color: 'black',
                                        fontSize: 16
                                    }
                                }
                            }
                        }
                    }
                ]
            })
            if(options) {
                that.myChart.setOption(options)
            }
        })
        return this
    }

    this.barLateralGetData = function(url, key, options) {
        key = key || {}
        const that = this
        options = options || null
        this.$.getJSON(url, {key:key}, function(d) {
            if(d.code != 0) {
                layer.msg(d.msg)
                return false
            }

            const data = d.data
            const x = data.x
            const y = data.y

            that.myChart.setOption({
                xAxis:{
                    type: 'value'
                },
                yAxis: {
                    type: 'category',
                    data:x,
                },
                series: [
                    {
                        type: 'bar',
                        data: y,
                        backgroundStyle: {
                            color: 'rgba(180, 180, 180, 0.2)'
                        },
                        itemStyle: {
                            normal: {
                                label: {
                                    show: true, //开启显示
                                    position: 'right', //在上方显示
                                    textStyle: { //数值样式
                                        color: 'black',
                                        fontSize: 16
                                    }
                                }
                            }
                        }
                    }
                ]
            })
            if(options) {
                that.myChart.setOption(options)
            }
        })
        return this
    }

    this.clone = function() {
        return new EchartsSelf(this.$);
    }

    //切换按钮改变状态
    this.changeStatus = function(url,data,table_id, msg) {
        var $ = this.$;
        var layui = this.layui;

        table_id = table_id || this.table_id;

        if(!msg && typeof data=='string' && data.indexOf('确定') !== -1) {
            //msg可以在data位置传过来
            msg = data;
            data = false;
        }
        msg = msg || '确定要变更本条数据的状态吗?';


        layer.confirm(msg, function(index) {
            layer.close(index);

            if (data) {
                var id = data.elem.name;
                var status = data.elem.checked;
                if (status == true) status = 1;
                else status = 0;
                url = url + '?id=' + id + '&status=' + status;
            }
            //向服务端发送删除指令
            $.getJSON(url, function (d) {
                layer.msg(d.msg);
                if (d.code == 0) {
                    layui.table.reload(table_id);
                }
            });
        },function(index) {
            //取消时候的处理
            if(!!data.elem) {
                //开关.存在data.elem
                data.elem.checked = data.elem.checked ? false : true;
                layui.form.render();
            } else {
                //普通按钮.直接关闭index
                layer.close(index);
            }
        });
    }

}